package com.zz.cc.owner.app;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.baidu.mapapi.map.MapView;
import com.zz.cc.owner.R;
import com.zz.cc.owner.view.SummaryView;
import com.zz.common.app.BaseActivity;

public class MainActivity extends BaseActivity implements OnClickListener {
	private EditText mSearchInput;
	private Button mSearchBtn;
	
	private Button mReserveBtn;
	private Button mRechargeBtn;
	private Button mOtherBtn;
	
	private MapView mMapView;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initUI();
		fillView();
	}
	
	@Override
	public void onResume() {
		super.onResume();
		mMapView.onResume();
	}
	
	@Override
	public void onPause() {
		super.onPause();
		mMapView.onPause();
	}
	
	@Override
	public void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		mMapView.onRestoreInstanceState(savedInstanceState);
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mMapView.onSaveInstanceState(outState);
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		mMapView.destroy();
	}
	
	private void initUI() {
		mSearchInput = (EditText) findViewById(R.id.main_search_input);
		mSearchBtn = (Button) findViewById(R.id.main_search_btn);
		
		mMapView = (MapView) findViewById(R.id.main_map_view);
		
		mReserveBtn = (Button) findViewById(R.id.main_reserve);
		mRechargeBtn = (Button) findViewById(R.id.main_recharge);
		mOtherBtn = (Button) findViewById(R.id.main_other);
	}
	
	private void fillView() {
		mSearchBtn.setOnClickListener(this);

		mReserveBtn.setOnClickListener(this);
		mRechargeBtn.setOnClickListener(this);
		mOtherBtn.setOnClickListener(this);
		
		showSummaryAtPosition(0, 0);
	}
	
	private void showSummaryAtPosition(long lat, long lon) {
		SummaryView sv = (SummaryView) LayoutInflater.from(this).inflate(R.layout.layout_summary_view, null);
		
		String basicInfo = "深圳在水一方BMW维护" 
				+ "\n五星" 
				+ "\n" + getString(R.string.detail_origin_cost) + "200"
				+ "\n" + getString(R.string.detail_cc_cost) + "160";
		sv.setBasicInfo(basicInfo);
		
		String introduce = getString(R.string.detail_introduce) + "\t"
				+ "两只老虎两只老虎，跑得快跑得快，一只没有眼睛，一只尾巴，真奇怪，真奇怪！";
		sv.setIntroduce(introduce);
		sv.setOnClickListener(this);
		
		mMapView.addView(sv);
	}
	
	private void onSummaryClick() {
		Intent intent = new Intent(this, DetailActivity.class);
		startActivity(intent);
		overridePendingTransition(R.anim.activity_slide_right_in, R.anim.activity_slide_left_out);
	}
	
	private void onSearchBtnClick() {
		//TODO search
	}
	
	private void onReserveBtnClick() {
		Intent intent = new Intent(this, ReserveActivity.class);
		startActivity(intent);
		overridePendingTransition(R.anim.activity_slide_right_in, R.anim.activity_slide_left_out);
	}
	
	private void onOtherBtnClick() {
		Intent intent = new Intent(this, OtherActivity.class);
		startActivity(intent);
		overridePendingTransition(R.anim.activity_slide_right_in, R.anim.activity_slide_left_out);
	}
	
	private void onRechargeBtnClick() {
		Intent intent = new Intent(this, RechargeActivity.class);
		startActivity(intent);
		overridePendingTransition(R.anim.activity_slide_right_in, R.anim.activity_slide_left_out);
	}

	@Override
	public void onClick(View view) {
		int id = view.getId();
		switch(id) {
		case R.id.summary:
			onSummaryClick();
			break;
			
		case R.id.main_search_btn:
			onSearchBtnClick();
			break;
			
		case R.id.main_reserve:
			onReserveBtnClick();
			break;
			
		case R.id.main_recharge:
			onRechargeBtnClick();
			break;
			
		case R.id.main_other:
			onOtherBtnClick();
			break;
			
		default:
			break;
		}
	}
}
